{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "923f67db",
   "metadata": {},
   "source": [
    "| [01_base/08_循环结构_loop.ipynb](https://github.com/shibing624/python-tutorial/blob/master/01_base/08_循环结构_loop.ipynb)  | Python循环  |[Open In Colab](https://colab.research.google.com/github/shibing624/python-tutorial/blob/master/01_base/08_循环结构_loop.ipynb) |\n",
    "\n",
    "\n",
    "# 循环\n",
    "\n",
    "## for循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "2f9b7ff3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4999950000\n"
     ]
    }
   ],
   "source": [
    "# for 循环\n",
    "total = 0\n",
    "for i in range(100000):\n",
    "    total += i\n",
    "print(total)  # 4999950000"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c363515",
   "metadata": {},
   "source": [
    "## while 循环\n",
    "\n",
    "```python\n",
    "while <condition>:\n",
    "     <statesments>\n",
    "    \n",
    "```\n",
    "\n",
    "Python会循环执行<statesments>，直到<condition>不满足为止。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2171e648",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5050\n"
     ]
    }
   ],
   "source": [
    "i = 0\n",
    "total = 0\n",
    "while i <= 100:\n",
    "    total += i\n",
    "    i += 1\n",
    "print(total)  # 5050"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0cb55b14",
   "metadata": {},
   "source": [
    "举个例子，通过while遍历集合："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "89780453",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Perform Hamlet\n",
      "Perform King\n",
      "Perform Mac\n"
     ]
    }
   ],
   "source": [
    "# 空容器会被当成False，因此可以用while循环读取容器的所有元素\n",
    "plays = set(['Hamlet', 'Mac', 'King'])\n",
    "while plays:\n",
    "    play = plays.pop()\n",
    "    print('Perform', play)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "663c2baa",
   "metadata": {},
   "source": [
    "## continue 语句\n",
    "\n",
    "遇到 continue 的时候，程序会返回到循环的最开始重新执行。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "405429ed",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n",
      "4\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "values = [7, 6, 4, 7, 19, 2, 1]\n",
    "for i in values:\n",
    "    if i % 2 != 0:\n",
    "        # 忽略奇数\n",
    "        continue\n",
    "    print(i)\n",
    "# 6\n",
    "# 4\n",
    "# 2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "396e4744",
   "metadata": {},
   "source": [
    "## break 语句\n",
    "\n",
    "遇到 break 的时候，程序会跳出循环，不管循环条件是不是满足"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "4b625093",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "start\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "command_list = ['start',\n",
    "                '1',\n",
    "                '2',\n",
    "                '3',\n",
    "                '4',\n",
    "                'stop',\n",
    "                'restart',\n",
    "                '5',\n",
    "                '6']\n",
    "while command_list:\n",
    "    command = command_list.pop(0)\n",
    "    if command == 'stop':\n",
    "        break\n",
    "    print(command)\n",
    "# start\n",
    "# 1\n",
    "# 2\n",
    "# 3\n",
    "# 4\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c5f20b79",
   "metadata": {},
   "source": [
    "本节完。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}